﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OutPrimeNumber
{
    class Program
    {
        //输出素数
        public void Calculate()
        {
            Console.WriteLine("请输入判断的数字总数：");

            int n;
            n = int.Parse(Console.ReadLine());

            Console.WriteLine("请输入判断的数字：");
            int[] testNumbers = new int[n];
            for (int i = 0; i < testNumbers.Length; i++)
            {
                testNumbers[i] = int.Parse(Console.ReadLine());
            }

            //判断素数，输出
            foreach (int number in testNumbers)
            {
                Boolean flag = true;
                for(int t = 2; t <number;t++ )
                {
                    if (number%t == 0)
                    {
                        flag = false;
                        break;
                    }
                }
                if(flag == true)
                {
                    Console.WriteLine(number + "  是素数");
                }
            }

            Console.ReadLine();

        }


        //求最大数值，最小值，平均值，和
        public void ArrayCalculate()
        {
            Console.WriteLine("请输入数组长度：");

            int n;
            n = int.Parse(Console.ReadLine());

            Console.WriteLine("请输入数组数据：");
            int[] testNumbers = new int[n];
            for (int i = 0; i < testNumbers.Length; i++)
            {
                testNumbers[i] = int.Parse(Console.ReadLine());
            }

            int max=testNumbers[0], min = testNumbers[0], mean = 0, sum = 0;
            //求最大值最小值平均数和
            foreach (int number in testNumbers)
            {
                if (number > max)
                    max = number;
                if (number < min)
                    min = number;
                sum += number;
            }
            Console.WriteLine("最大值为：" + max);
            Console.WriteLine("最小值为：" + min);
            Console.WriteLine("平均值为：" + sum/n);
            Console.WriteLine("和为：" + sum);
            Console.ReadLine();
        }

        //求2-100内素数，埃式筛法
        public void AiCalculate()
        {
            int[] numbers = new int[99];
            for(int i = 0; i < numbers.Length; i++)
            {
                numbers[i] = i + 2;
            }
            //埃式筛法
            for(int n = 2; n <= 50; n++)
            {
                for (int i = 0; i < numbers.Length; i++)
                {
                    if (numbers[i] % n == 0 && numbers[i] != n)
                        numbers[i] = 0;
                }
            }
            //输出结果
            for (int i = 0; i < numbers.Length; i++)
            {
                if (numbers[i] != 0)
                    Console.WriteLine(numbers[i] + "是素数");
            }
            Console.ReadLine();
        }

        //判断是否为托普利茨矩阵
        public Boolean CheckToeplitzMatrix()
        {

            Console.WriteLine("请输入矩阵M大小：");
            int M;
            M = int.Parse(Console.ReadLine());

            Console.WriteLine("请输入矩阵N大小：");
            int N;
            N = int.Parse(Console.ReadLine());

            //输入数组
            int[,] matrix = new int[N,M];
            for (int a = 0; a < N; a++) {
                for (int i = 0; i < M; i++)
                {
                    matrix[a,i] = int.Parse(Console.ReadLine());
                }
            }

            //检查
            for (int a = 0; a < N; a++)
            {
                for (int i = 0; i < M; i++)
                {
                    Console.Write(matrix[a, i]);
                }
                Console.WriteLine();
            }

            //判断
            for (int a = 0; a < N; a++)
            {
                for (int i = 0; i < M; i++)
                {
                    if (a + 1 < N && i + 1 < M)
                    {
                        if (matrix[a,i] != matrix[a + 1, i + 1])
                        {
                            Console.WriteLine("该矩阵不是托普利茨矩阵");
                            Console.ReadLine();
                            return false;
                        }
                    }
                }
            }
            Console.WriteLine("该矩阵是托普利茨矩阵");
            Console.ReadLine();
            return true;
        }

        static void Main(string[] args)
        {
            Program p = new Program();
            //输出素数
            p.Calculate();
            //求最大数值，最小值，平均值，和
            p.ArrayCalculate();
            //求2-100内素数，埃式筛法
            p.AiCalculate();
            //判断是否为托普利茨矩阵
            p.CheckToeplitzMatrix();
        }
    }
}
